import {
	isEmpty,
	resetForm
} from '@/utils/common.js'
import uniCopy from '@/js_sdk/xb-copy/uni-copy.js'

export default {
	data() {
		return {
			isShow: false,
			state: null, // 0揽收 1发出 2到达 3待取件 4问题件 5签收
			awaitNumbers: '', // 待检测的单号
			errorNumbers: '', // 未通过验证的单号
			form: {
				numbers: '', // 单号
				state: null, // 状态
				notes: '', // 备注
				laosNotes: '',
				currentUserId: null, // 当前网点
				currentUserName: '',
				currentName: '',
				laosCurrentName: '',
				sendToUserId: null, // 发往网点
				sendToUserName: '',
				sendToName: '',
				laosSendToName: ''
			},
			rules: {
				sendToUserId: {
					required: true,
					type: 'number',
					message: '请选择发往网点',
					trigger: 'change'
				}
			},
			sendShow: false,
			sendSite: [],
			submitBtnStyle: {
				width: '690rpx',
				color: '#FFFFFF',
				height: '80rpx',
				lineHeight: '80rpx',
				overflow: 'hidden',
				letterSpacing: '2rpx',
				fontSize: '34rpx',
				fontWeight: '500',
				backgroundImage: 'linear-gradient(to right, #F7B314, #F5D547)',
				boxShadow: '0 3px 6px 0 rgba(247,179,50,0.33)'
			},
			labelStyle: {
				color: '#666',
				fontSize: '28rpx'
			}
		}
	},
	computed: {
		site() {
			return this.$store.getters.site
		}
	},
	mounted() {
		this.init()
	},
	methods: {
		init() {
			this.$refs.uForm.setRules(this.rules);
			this.isShow = true;
		},
		
		// 检测单号 1
		detection() {
			let value = this.awaitNumbers.trim()
			let list = [];
			value = value.split(','); // 分割
			value.forEach(item => { // 去空格
				item = item.trim();
				if (item !== '') list.push(item)
			});
			for (let i = 0; i < list.length; i++) { // 去重
				for (let j = i + 1; j < list.length; j++) {
					if (list[i] === list[j]) {
						list.splice(j, 1);
						j--;
					}
				}
			}
			let numbers = list.join(',')
			if (!numbers) {
				uni.showToast({
					title: '单号不能为空',
					icon: 'none'
				})
				return
			}
			this.detectionSecond(numbers)
		},

		// 检测单号 2
		detectionSecond(numbers) {
			if (!numbers) return
			let param = {
				state: this.state,
				numbers: numbers
			};
			uni.showLoading()
			this.$api.checkApi(param).then(result => {
				uni.hideLoading()
				let reponse = result.resultParam
				// 通过
				this.form.numbers = reponse.successList.join(',')
				// 未通过
				let errList = []
				for (let key in reponse.errorMap) {
					if (reponse.errorMap[key].length !== 0)
						errList = [...errList, ...reponse.errorMap[key]]
				}
				this.errorNumbers = errList.join(',')
			}).catch(() => {
				uni.hideLoading()
			})
		},

		setCurrent() {
			this.form.currentUserId = this.site.id;
			this.form.currentUserName = this.site.username;
			this.form.currentName = this.site.name;
			this.form.laosCurrentName = this.site.laosName;
		},

		// 扫描获取单号
		scan() {
			let _this = this
			uni.scanCode({
				scanType: ['barCode'],
				success: function(res) {
					let numbers = _this.awaitNumbers
					let newNumber = res.result
					numbers = numbers.trim()
					if (!numbers) {
						_this.awaitNumbers = newNumber
					} else {
						_this.awaitNumbers = numbers + ',' + newNumber
					}
				}
			})
		},

		// 复制
		copy() {
			let content = this.errorNumbers
			if (!content) return
			uniCopy({
				content: content,
				success: () => {
					uni.showToast({
						title: '复制成功',
						icon: 'none'
					})
				}
			})
		},

		// 获得所有网点
		getAllSite() {
			let data = {
				username: ''
			}
			this.$api.listAllUserApi(data).then(result => {
				this.sendSite = result.resultParam.userList
			})
		},
		// 选择发往网点
		selectSend(index) {
			let obj = this.sendSite[index]
			this.form.sendToUserId = obj.id
			this.form.sendToName = obj.name
			this.form.laosSendToName = obj.laosName
			this.form.sendToUserName = obj.username
		},

		// 提交
		submit() {
			let _this = this
			this.setCurrent()
			if (!this.form.numbers) {
				this.$refs.uToast.show({
					title: '无法提交，无通过单号'
				})
				return
			}
			this.$refs['uForm'].validate(valid => {
				if (!valid) {
					this.$refs.uToast.show({
						title: '无法提交，有必填项未填写'
					})
					return;
				}
				uni.showModal({
					title: '确认提交？',
					content: '',
					success: function(res) {
						if (res.confirm) {
							let data = {
								..._this.form
							}
							for (let key in data) {
								if (!data[key]) delete data[key]
							}
							data.state = _this.state
							uni.showLoading();
							_this.$api.addCourierOperationApi(data).then(result => {
								uni.hideLoading();
								if (result.status !== 200) return;
								_this.cancel()
							}).catch(() => {
								uni.hideLoading();
							});
						}
					}
				})
			})
		},

		// 关闭
		cancel() {
			let errorNumbers = this.errorNumbers
			resetForm(this).then(() => {
				this.errorNumbers = errorNumbers
			})
		}
	}
}
